<template>
  <div class="app-container">
    <el-steps
      :active="2"
      process-status="wait"
      align-center
      style="margin-bottom: 40px"
    >
      <el-step title="步骤1" description="编辑基本信息"></el-step>
      <el-step title="步骤2" description="上传详细信息"></el-step>
      <el-step title="步骤3" description="发布课程"></el-step>
    </el-steps>
    <el-form
      ref="teacher"
      :model="course"
      label-width="80px"
      :label-position="labelPosition"
    >
      <el-form-item label="课程封面">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="'http://120.27.131.93:8080/eduoss/fileoss/upload'"
          class="avatar-uploader"
        >
          <img :src="course.headshotUrl" style="width: 300px; height: 300px" />
        </el-upload>
      </el-form-item>
      <el-form-item label="课程简介">
        <el-input
          v-model="course.introduction"
          type="textarea"
          :rows="6"
          autocomplete="off"
          maxlength="50"
          show-word-limit
        ></el-input>
      </el-form-item>
    </el-form>

    <div class="btn">
      <el-button type="primary" @click="saveCourse()">立即创建</el-button>
      <el-button>取消</el-button>
    </div>
  </div>
</template>

<script>
import courseApi from "@/api/course";
import Tinymce from "@/components/Tinymce"; //引入组件
export default {
  components: { Tinymce },
  data() {
    return {
      labelPosition: "left",
      course: {
        
      },
    };
  },
  created() {
    //获取路由id值
    if (this.$route.params && this.$route.params.id) {
      this.course.coursesId = this.$route.params.id;
    }
    this.getCourseInfoById()

  },
  methods: {
    getCourseInfoById() {
      courseApi.getCourseById(this.course.coursesId).then((res) => {
        this.course.headshotUrl =  "https://zhanghao-edu.oss-cn-beijing.aliyuncs.com/2021/09/22/c8aaed56-3d9d-4c67-8ead-36089c32f735zh.jpg"
        this.course = res.data.course;
   
      });
    },
    saveCourse() {
      courseApi.updateCourse(this.course).then((res) => {
        //提示
        this.$message({
          type: "success",
          message: "编辑详细信息成功信息成功!",
        });
        //跳转到第二步
        this.$router.push({
          path: "/course/view/" + this.course.coursesId,
        });
      });
      console.log(this.course)
    },
    //上传封面成功调用的方法
    handleAvatarSuccess(res, file) {
      this.course.headshotUrl = res.data.url;
    },
    //上传之前调用的方法
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style>
.btn {
  margin: 10px 10px;
  text-align: center;
}
</style>